<div class="layuimini-main">

	<div class="layui-form layuimini-form">
		<div class="layui-form-item">
			<label class="layui-form-label required">权限名称</label>
			<div class="layui-input-block">
				<input type="text" name="name" lay-verify="required" lay-reqtext="权限名称不能为空" placeholder="请填写权限名称" value="" class="layui-input">
				<tip>填写权限的名称。</tip>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">权限code</label>
			<div class="layui-input-block">
				<input type="text" name="code" lay-verify="required" lay-reqtext="权限code不能为空" placeholder="请填写权限code" value=""
				 class="layui-input">
				<tip>填写权限的code。</tip>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">url</label>
			<div class="layui-input-block">
				<input type="text" name="url" l placeholder="请填写url地址" value="" class="layui-input">
				<tip>填写权限的url地址</tip>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">类型</label>
			<div class="layui-input-block">
				<select id="type_select" disabled lay-verify="required" lay-reqtext="必须选择类型">
					<option value=""></option>
					<option value="1">菜单</option>
					<option value="2">页面</option>
					<option value="3">接口</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item" pane="">
			<label class="layui-form-label">是否显示</label>
			<div class="layui-input-block">
				<input type="radio" name="state" value="1" title="显示" checked="">
				<input type="radio" name="state" value="-1" title="不显示">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">排序</label>
			<div class="layui-input-block">
				<input type="number" name="sort" placeholder="请输入排序" value="" class="layui-input">
				<tip>请填写排序。</tip>
			</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label">父菜单</label>
			<div class="layui-input-block">
				<div id="tree_select" class="xm-select-demo"></div>
			</div>
		</div>
		<input type="hidden" name="pid" />
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">备注信息</label>
			<div class="layui-input-block">
				<textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
</div>
<script>
	layui.use(['form', 'table', 'hsw'], function() {
		var form = layui.form,
			layer = layui.layer,
			table = layui.table,
			hsw = layui.hsw,
			$ = layui.$;

		// 当前弹出层，防止ID被覆盖
		var parentIndex = layer.index;

		//监听提交
		form.on('submit(saveBtn)', function(data) {
			//console.log(data.field);
			hsw.hsw_post(HSW_API_MANAGE_PEIMISSION_EDIT.replace('pid',data.field.pid),data.field, function(data) {
					//console.log(data);

					layer.msg('操作成功，即将刷新页面', {
						time: 3000 //
					}, function() {
						hsw.hsw_html_reload(0);
					});

					// 关闭弹出层
					layer.close(index);
					layer.close(parentIndex);

			});
			return false;
		});

		window.editInit = function(data) {
			console.log(data);
			$('input[name="pid"]').attr('value', data.pid);
			$('input[name="name"]').attr('value', data.name);
			$('input[name="code"]').attr('value', data.code);
			$('input[name="url"]').attr('value', data.url);
			$('#type_select').val(data.type);
			
			$("input[name=state][value=1]").attr("checked", data.state == 1 ? true : false);
			$("input[name=state][value=-1]").attr("checked", data.state == -1 ? true : false);
			
			$('input[name="sort"]').attr('value', data.sort);
			$('textarea[name="remark"]').val(data.remark);
			
			var get_tree_type = 3;
			/* 判断要加载的tree*/
			if(data.type ==1||data.type ==2){
				//类型是菜单只加载菜单Tree
				get_tree_type =1;
			}else {
				get_tree_type =2;
			}
			
			/* 初始化select*/
			hsw.hsw_post(HSW_API_MANAGE_PEIMISSION_TREE, {type:get_tree_type}, function(tree) {
							
				var tree = changeTreeData(tree,'pid','value','child','children');
				var treeSelect = hsw.hsw_tree_radio_select('tree_select','parentPid',tree);
				if(data.parentName != null){
					treeSelect.setValue([
							{name: data.parentName, value: data.parentPid}
						], null, true);
				}
							
			});
			
			
			form.render();
		}

	});
	
	/**
	     * 
	     * 使用正则来修改Tree型json数据中的key属性名：
	     * params arrayJsonObj <array> 需要修改的json格式的数组
	     * params newKey <string> 需要修改成的key值
	     * params oldKey <string> 需要被修改的key值
	     */
	    function changeTreeData(arrayJsonObj, oldKey, newKey,oldKey1, newKey2) {
	        let str = JSON.stringify(arrayJsonObj);
	        let reg = new RegExp(oldKey, 'g');
	        let newStr = str.replace(reg, newKey);
			let reg1 = new RegExp(oldKey1, 'g');
			let newStr1 = newStr.replace(reg1, newKey2);
	        return JSON.parse(newStr1);
	    }

</script>
